<template>
  <div>
    <el-dialog
      :title="flag.isAdd?'用户添加':'用户修改'"
      :visible.sync="flag.isShow"
      @closed="closed"
      @opened="opened"
    >
      <el-form :model="form">
        {{form}}
        <el-form-item label="上级分类" label-width="70px">
          <el-select v-model="form.pid" placeholder="选择上级分类">
            <el-option label="顶级分类" value="3"></el-option>
            <el-option
              :label="item.catename"
              :value="item.id"
              v-for="item in cateList"
              :key="item.id"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="分类名称" label-width="70px">
          <el-input v-model="form.catename" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="图片" label-width="70px">
          <el-upload
            class="avatar-uploader"
            action="#"
            :show-file-list="false"
            :on-change="changeFile"
          >
            <img v-if="imgUrl" :src="imgUrl" class="avatar" />
            <i v-else class="el-icon-plus avatar-uploader-icon"></i>
          </el-upload>
        </el-form-item>
        <el-form-item label="状态" label-width="70px">
          <el-switch
            v-model="form.status"
            active-color="#13ce66"
            :active-value="1"
            :inactive-value="2"
          ></el-switch>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="flag.isShow = false">取 消</el-button>
        <el-button type="primary" @click="add" v-if="flag.isAdd">添加</el-button>
        <el-button type="primary" @click="change" v-else>修改</el-button>
      </div>
    </el-dialog>
  </div>
</template>
<script>
import { mapGetters, mapActions } from "vuex";
import { cateAdd, cateEdit } from "../../../request/api";
export default {
  data() {
    return {
      form: {
        pid: "",
        catename: "",
        img: null,
        status: 1
      },
      imgUrl: ""
    };
  },
  props: ["flag"],
  computed: {
    ...mapGetters({
      cateList: "cate/cateList",
      cateInfo: "cate/cateInfo"
    })
  },
  components: {},
  methods: {
    ...mapActions({
      reqCateList: "cate/reqCateList"
    }),
    closed() {
      if(!this.flag.isAdd){
        this.clear()
      }
    },
    opened() {
      if (!this.flag.isAdd) {
        console.log(this.cateInfo);
        this.form = this.cateInfo;
        //给图片赋值
        this.imgUrl = this.$pre + this.form.img;
      }
    },
    change() {
      console.log(this.form);
      cateEdit(this.form).then(res=>{
        console.log(res.data);
      })
    },
    add() {
      console.log(this.form);
      cateAdd(this.form).then(res => {
        if (res.data.code == 200) {
          this.flag.isShow = false;
          this.clear();
          this.reqCateList();
        }
      });
    },
    //修改图片
    changeFile(e) {
      let file = e.raw;
      //根据文件生成一个图片地址
      this.imgUrl = URL.createObjectURL(file);
      //给form.img
      this.form.img = file;
      this.flag.isShow = false
    },
    clear() {
      this.form = {
        pid: "",
        catename: "",
        img: null,
        status: 1
      };
    }
  },
  mounted() {}
};
</script>
<style lang='less' scoped>
/deep/.avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}
.avatar-uploader .el-upload:hover {
  border-color: #409eff;
}
.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 178px;
  height: 178px;
  line-height: 178px;
  text-align: center;
}
.avatar {
  width: 178px;
  height: 178px;
  display: block;
}
</style>